//
//  EmoticonViewModel.swift
//  Emoticon
//
//  Created by 刘俊 on 15/10/27.
//  Copyright © 2015年 刘俊. All rights reserved.
//

import UIKit

class  EmoticonViewManger {
    
    lazy var packages = [Emoticons]()
    /// 单例
    static let shardManger = EmoticonViewManger()
    
    private init() {
        //0,添加默认分组
        packages.append(Emoticons(dict: ["group_name_cn": "最近"]))
        //1,加载emoticons.plist
        //在inDirectory目录下加载对应的plist文件,如果文件不存在,path为nil
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        //获取字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        //获取模型数组
        let array = dict["packages"] as! NSArray
        //读取对应的id数组
        let ids = array.valueForKey("id")
        //遍历数组加载对应的plist
        for id in ids as! [String]{
            loadInfoPlist(id)
        }
        
    }
    /// 根据id加载每一个info.plist
    private func loadInfoPlist(id: String) {
        //1,获取对应的info.plist的路径
        let path = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")!
        //2,获取对应的字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        //3,将字典转化为对应的模型
        let model = Emoticons(dict: dict)
        //4,将模型添加到packages数组中
        packages.append(model)
  
    }
}
